<?php

namespace app\common\model;


use think\Model;
use think\Db;

class UserModel extends Model
{

    protected $name = 'user';

    //获取好友对战
    public function get_friends_pk_user($room_id,$user_id,$to_user_id){
        //$is_online = Gateway::isUidOnline($to_user_id);
        //if(empty($is_online))\think\Cache::set($room_id.'is_entry','');
        $from_client_info = [];
        $to_client_info = [];

        $user_info = db('user')->field('id,openid,avatar,display_name,sex,city,grad_name,count_wins,exp,is_robot')->where('id','IN',[$user_id,$to_user_id])->order("field(id,'$user_id','$to_user_id')")->select();

        if(!empty($user_info[0])){
            $from_client_info = $user_info[0];
            $from_client_info['room_id'] = $room_id;
            $from_client_info['client_name'] = $user_info[0]['display_name'];
            $from_client_info['level'] = get_user_level($user_info[0]['exp']);
        }
        if(!empty($user_info[1])){    
            //
            $to_client_info = $user_info[1];
            $to_client_info['room_id'] = $room_id;
            $to_client_info['client_name'] = $user_info[1]['display_name'];
            $to_client_info['level'] = get_user_level($user_info[1]['exp']);

        }

        return ['from_client_info'=>$from_client_info,'to_client_info'=>$to_client_info];
    }

    //分享加入群
    public function user_join_group($user_id,$group_id){
        if(empty($user_id) || empty($group_id))return false;
        $group_info = Db::name('group')->where(['group_id'=>$group_id,'user_id'=> $user_id])->find();
        if(empty($group_info)){
            $data=[
                'user_id' => $user_id,
                'group_id' => $group_id,
                'create_time'=>time()
            ];
            $res = Db::name('group')->insertGetId($data);
            return $res; 
        }else{
            return true;
        }
    }

    //用户升级知识书及道具
    public function knowledge_up($user_id){
        $knowledge = Db::name('user_knowledge_up ku')
                ->join('knowledge_up_rule kur', 'ku.level=kur.level')
                ->where('ku.user_id', $user_id)
                ->field('kur.reward_scale,ku.subject_id')
                ->select();
        $jf_limit_num=Db::name('user_info_extend')->where('user_id',$user_id)->value('jf_limit_num');
        $use_prop['jf_limit_num'] = $jf_limit_num?$jf_limit_num:0;
        if($jf_limit_num>0){
            $use_prop['reward_scale']=Db::name('prop')->where('flag','jf_limit_num')->value('reward_scale');
        }else{
            $use_prop['reward_scale']=0;
        }
        $data_up['use_prop']=$use_prop;
        if($knowledge){
            foreach($knowledge as $k=>$v){
                $up_rule[$v['subject_id']]=$v['reward_scale']; 
            }
            $data_up['up_rule']=$up_rule;
        }else{
            $data_up=[
                'up_rule'=>[],
                'use_prop'=>[
                    'reward_scale'=>0,
                    'jf_limit_num'=>0,
                ]
            ];
        }
        return $data_up;
    }
    
}